package com.wpw.web.work.sql;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @ClassName: LocationNumSql
 * @CreateDate: 2020-05-06 23:00
 * @author: wupengwei
 */
public class LocationNumSql {

    //UPDATE db_procurement.t_location_product SET num = num + 1 WHERE location_code = '1111' AND product_id = 11 LIMIT 1;
    public static void main(String[] args) {

        String filePath = "/Users/wupengwei/Desktop/储位数据/储位数量.txt";
        Path path = Paths.get(filePath);
        try {
            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
            int totalSize = lines.size();
            System.out.println("共" + totalSize + "条数据");

            List<List<String>> dataList = new ArrayList<>();
            for (int i = 0; i < lines.size(); i++) {
                String data = lines.get(i);
                String[] split = data.split("\t");
                List<String> list = Arrays.asList(split);
                dataList.add(list);
            }

            String jsFile = "/Users/wupengwei/Desktop/储位数据/locaiontNum.sql";
            Path jsPath = Paths.get(jsFile);
            BufferedWriter writer = Files.newBufferedWriter(jsPath, StandardCharsets.UTF_8);
            for (List<String> list : dataList) {
                String num = list.get(0);
                String locationCode = list.get(1);
                String productId = list.get(2);
                StringBuilder sb = new StringBuilder("UPDATE db_procurement.t_location_product SET num = num + ")
                        .append(num)
                        .append(" WHERE location_code = '").append(locationCode).append("' AND product_id = ")
                        .append(productId).append(" LIMIT 1;");

                writer.write(sb.toString());
                writer.newLine();

            }

            writer.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}